package com.yx.cloud.map;

import android.view.MotionEvent;

import com.aaa.lib.map.LayerManager;
import com.aaa.lib.map.MapUtils;
import com.aaa.lib.map.MapView;
import com.aaa.lib.map.layer.BaseLayer;
import com.yx.cloud.map.layer.YXAreaLayer;
import com.yx.cloud.map.layer.YXForbiddenAreaLayer;
import com.yx.cloud.map.layer.YXForbiddenLineLayer;
import com.yx.cloud.map.layer.YXPowerLayer;
import com.iot.product.sweeper.bean.AreaInfo;
import com.iot.common.utils.LogUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class YXLayerManager extends LayerManager {
    private static final String TAG = "YXLayerManager";

    /**图层顺序
    房间标签
    AI识别/家具
    机器
    充电座
    禁区/划区/禁过
    路径
    地图
     */


    public YXLayerManager(MapView mapView) {
        super(mapView);
    }

    /**
     * 重新设置所有区域图层
     *
     * @param yxAreaLayers
     */
    public void setAreaLayer(List<YXAreaLayer> yxAreaLayers) {
        //移除所有区域图层
        Iterator<BaseLayer> it = mLayerList.iterator();
        while (it.hasNext()) {
            BaseLayer baseLayer=it.next();
            if (baseLayer instanceof YXAreaLayer && !(baseLayer instanceof YXPowerLayer)/*这里坑了我一个下午*/  ) {
                it.remove();
            }
        }
        //添加所有新获取的区域图层
        mLayerList.addAll(yxAreaLayers);
    }

    /**
     * 通过ID判断是否存在
     *
     * @param areaInfo
     * @return
     */
    public YXAreaLayer isAreaLayerExist(AreaInfo areaInfo) {
        for (BaseLayer layer : mLayerList) {
            if (layer instanceof YXAreaLayer && ((YXAreaLayer) layer).getAreaInfo().getId() == areaInfo.getId()) {
                return (YXAreaLayer) layer;
            }
        }
        return null;
    }

    /**
     * 获取选择的区域图层
     *
     * @return
     */
    /* public List<BaseLayer> getSelectedArea() {
       List<BaseLayer> layers = new ArrayList<>();
        for (BaseLayer layer : mLayerList) {
            if (layer instanceof YXAreaLayer && ((YXAreaLayer) layer).isSelected()) {
                layers.add(layer);
                if (!YXAreaLayer.isMultiSelect) {
                    //如果是单选 那么就结束
                    break;
                }
            }
        }
        return layers;
    }*/

    /**
     * 获取勾选的指定类型的区域图层
     *
     * @param cls
     * @return
     */
/*    public List<BaseLayer> getSelectedAreaByType(Class cls) {
        List<BaseLayer> layers = new ArrayList<>();
        for (BaseLayer layer : mLayerList) {
            if (layer.getClass() == cls && ((YXAreaLayer) layer).isSelected()) {
                layers.add(layer);
                if (!YXAreaLayer.isMultiSelect) {
                    //如果是单选 那么就结束
                    break;
                }
            }
        }
        return layers;
    }*/

    /**
     * 获取所有图层
     *
     * @return
     */
    public List<YXAreaLayer> getAllArea() {
        List<YXAreaLayer> layers = new ArrayList<>();
        for (BaseLayer layer : mLayerList) {
            if (layer.getClass() == YXAreaLayer.class) {
                layers.add((YXAreaLayer) layer);
            }
        }
        return layers;
    }

    /**
     * 获取指定类型的图层
     *
     * @param cls
     * @return
     */
    public List<BaseLayer> getLayerByType(Class cls) {
        List<BaseLayer> layers = new ArrayList<>();
        for (BaseLayer layer : mLayerList) {
            if (layer.getClass() == cls) {
                layers.add(layer);
            }
        }
        return layers;
    }
    public int getLayerCountByType(Class... cls) {
        int count=0;
        for (BaseLayer layer : mLayerList) {
            for(int i = 0; i < cls.length; ++i) {
                if (layer.getClass() == cls[i]) {
                    count++;
                    break;
                }
            }
        }
        return count;
    }

    /**
     * 判断禁区是否与电源保护区域相交
     *
     * @param powerLayer
     * @return
     */
    public boolean isPowerCrossForbidArea(YXPowerLayer powerLayer) {
        for (BaseLayer layer : mLayerList) {
            if (layer instanceof YXForbiddenAreaLayer) {
                //判断禁区是否与电源保护区域相交
                if (MapUtils.isAreaCrossPower(((YXForbiddenAreaLayer) layer).getArea(), powerLayer.getArea())) {
                    return true;
                }
            }else if (layer instanceof YXForbiddenLineLayer) {
                //判断禁过线是否与电源保护区域相交
                if (MapUtils.isLineCrossPower(((YXForbiddenLineLayer) layer).getArea(), powerLayer.getArea())) {
                    return true;
                }
            }

        }
        return false;
    }

    public void removeLayers(List layers) {
        mLayerList.removeAll(layers);
    }

    /**
     * 判断禁区是否改变
     *
     * @return
     */
/*    public boolean isForbiddenAreaChange() {
        for (BaseLayer layer : mLayerList) {
            if (layer instanceof YXForbiddenAreaLayer) {
                LogUtils.i( "isForbiddenAreaChange Area : "+ ((YXForbiddenAreaLayer) layer).isChanged());
                if (((YXForbiddenAreaLayer) layer).isChanged()) {
                    return true;
                }
            }else if (layer instanceof YXForbiddenLineLayer) {
                LogUtils.i( "isForbiddenAreaChange Line: "+((YXForbiddenLineLayer) layer).isChanged());
                if (((YXForbiddenLineLayer) layer).isChanged()) {
                    return true;
                }
            }
        }
        return false;
    }*/

    /**
     *
     * @return
     */
/*    public void setForbiddenAreaUnchange() {
        for (BaseLayer layer : mLayerList) {
            if (layer instanceof YXForbiddenAreaLayer) {
                ((YXForbiddenAreaLayer) layer).setChanged(false);
            }else if (layer instanceof YXForbiddenLineLayer) {
                ((YXForbiddenLineLayer) layer).setChanged(false);
            }
        }
    }*/


    @Override
    public boolean dispatchToLayers(MotionEvent event) {
        if (mLayerList == null) {
            return false;
        }
        //每个事件都对所有图层进行判断, 相比父类方法 会多处理很多次
        //主要是为了指哪扫哪功能 因为要点击位置就变化 不能消耗事件而又让down的后续事件能发过来处理
        for (int i = mLayerList.size() - 1; i > -1; i--) {
            BaseLayer layer = mLayerList.get(i);
            boolean handled = layer.onTouch(event);
            if (handled) {
                LogUtils.i("layer : "+ layer.getClass().getSimpleName() + " handle");
                return true;
            }
        }
        LogUtils.i("layer :  no layer handle");
        return false;
    }

    public List<BaseLayer> getLayerList(){
        return mLayerList;
    }
}

